from django.shortcuts import render
from django.template.loader import get_template
from django.template import Context
from django.http import HttpResponse
from submitpool.models import *
import json
from EMC.public import *
import csv
import time
import uuid

# Create your views here.
def submitpool(request):
    t = get_template('../templates/submitpool.tpl')
    html = t.render(Context({'current_date': 'ADMIN'}))
    return HttpResponse(html)
	

def submitpool_item_view(request):
    result = []
    table = Item.objects.all()
    for row in table:
        result.append({"SUBMITPOOL_ID":row.SUBMITPOOL_ID,"SERIAL_NR":row.SERIAL_NR, "PART_NR":row.PART_NR, "STATUS":row.STATUS, "RESULT":row.RESULT,})
    result1 = {"total":len(result),"rows":result}
    return HttpResponse(json.dumps(result1))

def submitpool_upload_view(request):
    t = get_template('../templates/submitpool_upload.tpl')
    html = t.render(Context({'current_date': 'ADMIN'}))
    return HttpResponse(html)    

def submitpool_upload(request):
    counts = {'inserted':0,'updated':0,'deleted':0}
    error = ""
    try:
        if 'station' in request.POST and request.POST['status']:
            cstatus = request.POST['status']
            if cstatus == 'KEYIN':
                sync_keyin()
            elif cstatus == 'REPAIR':
                sync_repair()
            elif cstatus == 'CLOSED':
                sync_close()
            else:
                error = "wrong status name"
    except Exception as e:
        error = e[0]    
    return HttpResponse(error)

def sync_keyin():
    pass

def sync_repair():
    pass

def sync_close():
    pass
    